using System;
using System.Collections;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace Circus.CodeEditor
{
	public class XPThemes
	{
		private class XPTheme
		{
			private const int EBP_BACKGROUND = 0;
			private const int EBP_NORMALGROUPBACKGROUND = 5;
			private const int EBP_NORMALGROUPCOLLAPSE = 6;
			private const int EBP_NORMALGROUPEXPAND = 7;
			private const int EBP_NORMALGROUP = 8;
			private const int EBNG_NORMAL = 1;
			private const int EBNG_HOT = 2;
			private const int TS_DRAW = 2;
			private const int TMT_TEXTCOLOR = 3803;
			private const int EP_EDITTEXT = 1;
			private const int ETS_NORMAL = 1;
			private const int BP_PUSHBUTTON = 1;
			private const int PBS_NORMAL = 1;
			private const int PBS_HOT = 2;
			private const int PBS_PRESSED = 3;
			private const int BP_CHECKBOX = 3;
			private const int CBS_UNCHECKEDNORMAL = 1;
			private const int CBS_UNCHECKEDHOT = 2;
			private const int CBS_CHECKEDNORMAL = 5;
			private string explorerBarTheme = "explorerbar";
			private string editTheme = "edit";
			private string buttonTheme = "Button";
			private string currentThemeName = string.Empty;
			private IntPtr handle = IntPtr.Zero;
			private Hashtable themeNames;
			internal bool themesAvailable = false;
			public string GetCurrentThemeName
			{
				get
				{
					int num = 260;
					IntPtr intPtr = Marshal.AllocHGlobal(num + 1);
					IntPtr intPtr2 = Marshal.AllocHGlobal(num + 1);
					string result;
					try
					{
						Win32.GetCurrentThemeName(intPtr, num, intPtr2, num, IntPtr.Zero, IntPtr.Zero);
						result = Marshal.PtrToStringAuto(intPtr2);
					}
					finally
					{
						Marshal.FreeHGlobal(intPtr);
						Marshal.FreeHGlobal(intPtr2);
					}
					return result;
				}
			}
			private void CheckThemes()
			{
				this.themesAvailable = (OSFeature.Feature.GetVersionPresent(OSFeature.Themes) != null && Win32.IsAppThemed() && Win32.IsThemeActive() && (Win32.GetThemeAppProperties() & 2) == 2);
			}
			public XPTheme()
			{
				this.themeNames = new Hashtable();
			}
			public void Close()
			{
				this.currentThemeName = string.Empty;
				this.themeNames.Clear();
				if (this.handle != IntPtr.Zero)
				{
					Win32.CloseThemeData(this.handle);
				}
			}
			public void DrawGroupBackground(Painter painter, Rectangle rect)
			{
				this.OpenTheme(this.explorerBarTheme);
				this.DrawThemeBackground(painter, 5, 0, rect);
			}
			public void DrawOutbarBackground(Painter painter, Rectangle rect)
			{
				this.OpenTheme(this.explorerBarTheme);
				this.DrawThemeBackground(painter, 0, 0, rect);
			}
			public void DrawCheckBox(Painter painter, Rectangle rect, bool pressed, bool hot)
			{
				if (this.themesAvailable)
				{
					int stateID = pressed ? 5 : (hot ? 2 : 1);
					this.OpenTheme(this.buttonTheme);
					this.DrawThemeBackground(painter, 3, stateID, rect);
				}
			}
			public void DrawPushButton(Painter painter, Rectangle rect, bool pressed, bool hot)
			{
				if (this.themesAvailable)
				{
					int stateID = pressed ? 3 : (hot ? 2 : 1);
					this.OpenTheme(this.buttonTheme);
					this.DrawThemeBackground(painter, 1, stateID, rect);
				}
			}
			public void DrawThemeBackground(Painter painter, int partID, int stateID, Rectangle rect)
			{
				painter.DrawThemeBackground(this.handle, partID, stateID, rect);
			}
			public void DrawThemeBackground(IntPtr dc, int partID, int stateID, Rectangle rect)
			{
				Win32.GdiRect gdiRect = new Win32.GdiRect(rect);
				Win32.DrawThemeBackground(this.handle, dc, partID, stateID, ref gdiRect, IntPtr.Zero);
			}
			public void OpenTheme(string name)
			{
				if (!(this.currentThemeName == name))
				{
					object obj = this.themeNames[name];
					if (obj != null)
					{
						this.handle = (IntPtr)obj;
					}
					else
					{
						this.handle = Win32.OpenThemeData(IntPtr.Zero, name);
						this.themeNames.Add(name, this.handle);
					}
					this.currentThemeName = name;
				}
			}
			public void DrawEditBorder(Painter painter, Rectangle rect)
			{
				if (this.themesAvailable)
				{
					this.OpenTheme(this.editTheme);
					this.DrawThemeBackground(painter, 1, 1, rect);
				}
			}
			public void DrawEditBorder(IntPtr dc, Rectangle rect)
			{
				if (this.themesAvailable)
				{
					this.OpenTheme(this.editTheme);
					this.DrawThemeBackground(dc, 1, 1, rect);
				}
			}
			public Color GetGroupCaptionColor(bool hot)
			{
				Win32.ColorRef colorRef;
				Win32.GetThemeColor(this.handle, 8, 0, 3803, out colorRef);
				return colorRef.Color;
			}
			public void Open(string name)
			{
				this.CheckThemes();
				if (this.themesAvailable && name != string.Empty)
				{
					this.handle = Win32.OpenThemeData(IntPtr.Zero, name);
				}
			}
		}
		private static XPThemeName currentTheme;
		private static XPThemes.XPTheme theme;
		private static Point offset;
		public static Point Offset
		{
			get
			{
				return XPThemes.offset;
			}
			set
			{
				XPThemes.offset = value;
			}
		}
		public static XPThemeName CurrentTheme
		{
			get
			{
				return XPThemes.currentTheme;
			}
		}
		private static void DetectCurrentTheme()
		{
			string getCurrentThemeName = XPThemes.theme.GetCurrentThemeName;
			if (getCurrentThemeName == null || getCurrentThemeName.Length == 0)
			{
				XPThemes.currentTheme = XPThemeName.Blue;
			}
			else
			{
				if (getCurrentThemeName == "NormalColor")
				{
					XPThemes.currentTheme = XPThemeName.Blue;
				}
				else
				{
					if (getCurrentThemeName == "HomeStead")
					{
						XPThemes.currentTheme = XPThemeName.HomeStead;
					}
					else
					{
						if (getCurrentThemeName == "Metallic")
						{
							XPThemes.currentTheme = XPThemeName.Metallic;
						}
						else
						{
							XPThemes.currentTheme = XPThemeName.Custom;
						}
					}
				}
			}
		}
		static XPThemes()
		{
			XPThemes.currentTheme = XPThemeName.None;
			XPThemes.theme = new XPThemes.XPTheme();
			XPThemes.InitTheme();
		}
		public static Color GetGroupCaptionColor(bool hot)
		{
			Color result;
			switch (XPThemes.currentTheme)
			{
			case XPThemeName.None:
			case XPThemeName.Blue:
				if (hot)
				{
					result = Consts.DefaultXPItemsForeColor;
				}
				else
				{
					result = Consts.DefaultXPHeaderForeColor;
				}
				break;
			case XPThemeName.HomeStead:
				if (hot)
				{
					result = Color.FromArgb(114, 146, 29);
				}
				else
				{
					result = Color.FromArgb(86, 102, 45);
				}
				break;
			case XPThemeName.Metallic:
				if (hot)
				{
					result = Color.FromArgb(126, 124, 124);
				}
				else
				{
					result = Color.FromArgb(63, 61, 61);
				}
				break;
			default:
				result = XPThemes.theme.GetGroupCaptionColor(hot);
				break;
			}
			return result;
		}
		public static Color GetGroupItemCaptionColor(bool hot)
		{
			return XPThemes.GetGroupCaptionColor(hot);
		}
		public static void DrawGroupBackground(Painter painter, Rectangle rect)
		{
			if (painter != null)
			{
				switch (XPThemes.currentTheme)
				{
				case XPThemeName.None:
					painter.FillRectangle(Consts.BlueItemsAreaColor, rect);
					return;
				case XPThemeName.HomeStead:
					painter.FillRectangle(Consts.HomeSteadItemsAreaColor, rect);
					return;
				case XPThemeName.Metallic:
					painter.FillRectangle(Consts.MetallicItemsAreaColor, rect);
					return;
				}
				XPThemes.theme.DrawGroupBackground(painter, XPThemes.DrawRect(rect));
			}
		}
		public static void DrawBackground(Painter painter, Rectangle rect, Color startColor, Color endColor)
		{
			if (painter != null)
			{
				if (XPThemes.currentTheme == XPThemeName.None)
				{
					painter.FillGradient(rect, startColor, endColor, rect.Location, new Point(0, rect.Height));
				}
				else
				{
					XPThemes.theme.DrawOutbarBackground(painter, XPThemes.DrawRect(rect));
				}
			}
		}
		public static Rectangle DrawRect(Rectangle sourceRect)
		{
			return new Rectangle(sourceRect.X + XPThemes.offset.X, sourceRect.Y + XPThemes.offset.Y, sourceRect.Width, sourceRect.Height);
		}
		public static void DoneTheme()
		{
			XPThemes.theme.Close();
		}
		public static void InitTheme()
		{
			XPThemes.theme.Open(string.Empty);
			if (XPThemes.theme.themesAvailable)
			{
				XPThemes.DetectCurrentTheme();
			}
			else
			{
				XPThemes.currentTheme = XPThemeName.None;
			}
		}
		public static void DrawEditBorder(Painter painter, Rectangle rect)
		{
			XPThemes.theme.DrawEditBorder(painter, rect);
		}
		public static void DrawEditBorder(IntPtr dc, Rectangle rect)
		{
			XPThemes.theme.DrawEditBorder(dc, rect);
		}
		public static void DrawPushButton(Painter painter, Rectangle rect, bool pressed, bool hot)
		{
			XPThemes.theme.DrawPushButton(painter, rect, pressed, hot);
		}
		public static void DrawCheckBox(Painter painter, Rectangle rect, bool pressed, bool hot)
		{
			XPThemes.theme.DrawCheckBox(painter, rect, pressed, hot);
		}
	}

    public enum XPThemeName {
        None,
        Blue,
        HomeStead,
        Metallic,
        Custom
    }
}
